const { query } = require('../config/database-sqlite');

// 插入基础测试数据
const seedData = async () => {
  try {
    console.log('🌱 开始插入测试数据...');

    // 插入歌手数据
    const artists = [
      ['周杰伦', 'Jay Chou', '中国台湾', '华语流行音乐之王'],
      ['陈奕迅', 'Eason Chan', '中国香港', '香港著名歌手'],
      ['邓丽君', 'Teresa Teng', '中国台湾', '华语歌坛传奇']
    ];

    for (const [name, alias, country, description] of artists) {
      await query(
        'INSERT OR IGNORE INTO artists (name, alias, country, description) VALUES (?, ?, ?, ?)',
        [name, alias, country, description]
      );
    }
    console.log('✅ 歌手数据插入完成');

    // 插入歌曲数据 - 包含夜曲、青花瓷、七里香
    const songs = [
      ['稻香', 1, null, 223, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185008.mp3'],
      ['青花瓷', 1, null, 238, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185009.mp3'],
      ['夜曲', 1, null, 221, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185010.mp3'],
      ['七里香', 1, null, 252, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185011.mp3'],
      ['告白气球', 1, null, 214, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185012.mp3'],
      ['晴天', 1, null, 269, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185013.mp3'],
      ['简单爱', 1, null, 264, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185014.mp3'],
      ['听妈妈的话', 1, null, 262, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185015.mp3'],
      ['孤勇者', 2, null, 256, '/assets/default-cover.svg', 'https://music.163.com/song/media/outer/url?id=185016.mp3']
    ];

    for (const [name, artist_id, album_id, duration, cover_url, file_url] of songs) {
      await query(
        'INSERT OR IGNORE INTO songs (name, artist_id, album_id, duration, cover_url, file_url) VALUES (?, ?, ?, ?, ?, ?)',
        [name, artist_id, album_id, duration, cover_url, file_url]
      );
    }
    console.log('✅ 歌曲数据插入完成');

    // 插入歌单数据
    const playlists = [
      ['热门推荐', '精选热门歌曲', '/assets/playlist-hot.svg', null, 1, 0, 8],
      ['新歌首发', '最新发布的歌曲', '/assets/playlist-new.svg', null, 1, 0, 7],
      ['经典老歌', '经典怀旧歌曲', '/assets/playlist-classic.svg', null, 1, 0, 8],
      ['欧美流行', '欧美流行音乐', '/assets/playlist-pop.svg', null, 1, 0, 7],
      ['K-POP精选', '韩国流行音乐', '/assets/playlist-kpop.svg', null, 1, 0, 7],
      ['影视原声', '电影电视剧音乐', '/assets/playlist-movie.svg', null, 1, 0, 7]
    ];

    for (const [name, description, cover_url, user_id, is_public, play_count, song_count] of playlists) {
      await query(
        'INSERT OR IGNORE INTO playlists (name, description, cover_url, user_id, is_public, play_count, song_count) VALUES (?, ?, ?, ?, ?, ?, ?)',
        [name, description, cover_url, user_id, is_public, play_count, song_count]
      );
    }
    console.log('✅ 歌单数据插入完成');

    // 插入歌单歌曲关联数据 - 热门推荐歌单包含周杰伦的歌曲
    const playlistSongs = [
      [1, 1, 0], // 热门推荐 - 稻香
      [1, 2, 1], // 热门推荐 - 青花瓷
      [1, 3, 2], // 热门推荐 - 夜曲
      [1, 4, 3], // 热门推荐 - 七里香
      [1, 5, 4], // 热门推荐 - 告白气球
      [1, 6, 5], // 热门推荐 - 晴天
      [1, 7, 6], // 热门推荐 - 简单爱
      [1, 8, 7], // 热门推荐 - 听妈妈的话
    ];

    for (const [playlist_id, song_id, sort_order] of playlistSongs) {
      await query(
        'INSERT OR IGNORE INTO playlist_songs (playlist_id, song_id, sort_order) VALUES (?, ?, ?)',
        [playlist_id, song_id, sort_order]
      );
    }
    console.log('✅ 歌单歌曲关联数据插入完成');

    console.log('🎉 测试数据插入完成！');

  } catch (error) {
    console.error('❌ 数据插入失败:', error);
  }
};

if (require.main === module) {
  seedData();
}

module.exports = seedData;

